﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Yugi.Client;
using Yugi.Client.Control;
namespace Yugi_Gui
{
	/// <summary>
	/// Interaction logic for Tomb.xaml
	/// </summary>
	public partial class Tomb : UserControl, IInvoke
	{
        TombModel model;
		public Tomb()
		{
			this.InitializeComponent();
            this.DataContextChanged += new DependencyPropertyChangedEventHandler(Tomb_DataContextChanged);
		}

        void Tomb_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            this.model = e.NewValue as TombModel;
            if (model == null) return;
            model.Presenter = this;
        }             

        public void Add(CardModel card)
        {
            HandImage img = this.Add(card.YCard.displayImage);
            img.DataContext = card;
        }

        public HandImage Add(ImageSource source)
        {
            HandImage img = new HandImage();
            img.img.Source = source;
            this.LayoutRoot.Children.Add(img);
            return img;
        }
        public void Delete(int id)
        {
            HandImage img = null;
            foreach (HandImage item in this.LayoutRoot.Children)
            {
                if ( ((CardModel)item.DataContext).YCard.ID==id )
                {
                    img = item;
                    break;
                }
            }
            if (img != null)
                this.LayoutRoot.Children.Remove(img);
        }
        public void Delete(CardModel card)
        {
            HandImage img = null;
            foreach (HandImage item in this.LayoutRoot.Children)
            {
                if (item.DataContext == card)
                {
                    img = item;
                    break;
                }
            }
            if(img!=null)
                this.LayoutRoot.Children.Remove(img);
            
        }
        public HandImage Find(int id)
        {
            foreach (HandImage item in this.tombChoices.GetListChild())
            {
                if (((CardModel)item.DataContext).YCard.ID == id)
                {
                    return item;
                }
            }
            return null;
        }
        private void Load()
        {
            this.tombChoices.Clear();
            foreach (HandImage item in this.LayoutRoot.Children)
            {
                this.tombChoices.Add(item.DataContext as CardModel);
            }
        }
      
        #region Common Event
        
       
            private void UserControl_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
            {
                if (this.State == MState.Hide)
                {
                    this.tombChoices.Visibility = Visibility.Visible;
                    this.State = MState.Show;
                    this.Load();
                    this.tombChoices.Focus();
                    
                }
                else
                {
                    this.State = MState.Hide;   
                }
            }
          

            private void tombChoices_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
            {
                if (e.Key == Key.Escape)
                    this.State = MState.Hide;
            }

        #endregion

       

       

        private MState current = MState.Hide;
        public MState State
        {
            get
            {
                return current;
            }
            set
            {
                current = value;
                VisualStateManager.GoToState(this, current.ToString(), true);

            }
        }
        public enum MState
        {
            Show, Hide
        }



        #region IInvoke Members

        public void Do(ClientMSG msg, params object[] obj)
        {
            switch (msg)
            {
                case ClientMSG.ThemBaiVaoMo:
                    this.Add(obj[0] as CardModel);
                    break;
                case ClientMSG.XoaBaiTrongMo:
                    this.Delete(obj[0] as CardModel);
                    break;
                case ClientMSG.XoaBai:
                    int id = (int)obj[1];
                    this.Delete(id);
                    break;
                case ClientMSG.Reset:
                    Reset();
                    break;
            }
        }
        public void Refresh()
        {
            
        }
        #endregion

        public void Reset()
        {
            this.LayoutRoot.Children.Clear();
            model.Reset();
        }
    }
}